package com.spzx.user.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.core.domain.R;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.user.domain.UserAddress;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.user.service.IUserAddressService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 用户地址Controller
 *
 * @author atguigu
 * @date 2024-07-10
 */
@Tag(name = "用户地址接口管理")
@RestController
@RequestMapping("/userAddress")
public class UserAddressController extends BaseController
{
    @Resource
    private IUserAddressService userAddressService;
    @InnerAuth
    @Operation(summary = "根据地址id查询地址信息")
    @GetMapping("{id}")
    public R<UserAddress> getById(@PathVariable("id")Long id){
        return R.ok(userAddressService.getById(id));
    }


    /*
        {
  "id": null,
  "name": "z3t",
  "phone": "13522222222",
  "provinceCode": "320000",
  "cityCode": "320500",
  "districtCode": "320508",
  "address": "谷阳北路166号大江商厦401",
  "tagName": "家",
  "isDefault": 1,
  "default": {
    "value": true
  }
}
     */
    @Operation(summary = "新增用户的收货地址")
    @PostMapping
    public AjaxResult saveUserAddress(@RequestBody UserAddress userAddress){
        return toAjax(userAddressService.saveUserAddress(userAddress));
    }


    @Operation(summary = "结算页查询用户收货地址列表")
    @GetMapping("/list")
    public AjaxResult list(){
        //根据用户id查询他的收货地址列表
        // select * from user_address where del_flag = 0 and user_id = userId
        return success(userAddressService
                .list(Wrappers.lambdaQuery(UserAddress.class)
                        .eq(UserAddress::getUserId, SecurityUtils.getUserId())));
    }
}
